-module(bankclient).
-export([deposit/2, withdraw/2, balance/1]).

deposit(Who, X) -> simple_rpc({deposit, Who, X}).
withdraw(Who, X) -> simple_rpc({withdraw, Who, X}).
balance(Who) -> simple_rpc({balance, Who}).

simple_rpc(Command) ->
	case gen_tcp:connect("localhost", 3010, [binary, {packet, 4}]) of
		{ok, Socket} -> 
			io:format("connected to: ~p~n", [Socket]),
			% io:format("Sending Command: ~w~n", Command),
			% gen_tcp:send(Socket, term_to_binary(Command)),
			gen_tcp:send(Socket, <<"789">>),
			wait_reply(Socket)
	end.
	
wait_reply(Socket) ->
	receive
		{tcp, Socket, Bin} ->
			Term = Bin,
			gen_tcp:close(Socket),
			Term;
		{tcp_closed, Socket} ->
			true
	end.